using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Collections.Specialized;
using System.IO;
using System.Xml.Xsl;
using System.Text.RegularExpressions;
using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;
using LB.Generics;
using ExtraClub.Domain.Entity;
using ExtraClub.Domain.Framework;

namespace ExtraClub.Domain {
    public static class UIXmlManager {
        internal static Dictionary<string, string> GetUITextCollection(string path, string lang) {
            Dictionary<string, string> collection = null;
            XDocument doc = XDocument.Load(path);
            var q = from a in doc
                        .Elements("text")
                        .Elements("lang").Where(delegate(XElement item) { return item.Attribute("id").Value == lang; })
                        .Elements("item")
                    select a;


            if (q.Count() > 0) {
                collection = new Dictionary<string, string>();
                foreach (var item in q) {
                    string key = item.Attribute("id").Value;
                    string value = item.Attribute("value") != null ? item.Attribute("value").Value : item.Value;

                    if (!collection.ContainsKey(key)) {
                        collection.Add(key, value);
                    }
                }
            }
            return collection;
        }
        internal static Dictionary<string, string> GetUIStyleCollection(string path) {
            Dictionary<string, string> collection = null;
            XDocument doc = XDocument.Load(path);
            var q = from a in doc
                        .Elements("style")
                        .Elements("item")
                    select a;


            if (q.Count() > 0) {
                collection = new Dictionary<string, string>();
                foreach (var item in q) {
                    string key = item.Attribute("id").Value;
                    string value = item.Attribute("value") != null ? item.Attribute("value").Value : item.Value;

                    if (!collection.ContainsKey(key)) {
                        collection.Add(key, value);
                    }
                }
            }
            return collection;
        }
        internal static Dictionary<string, List<string>> GetSelectOptions(string path, string lang) {
            Dictionary<string, List<string>> selects = null;
            XDocument doc = XDocument.Load(path);
            var q = from a in doc
                        .Element("text")
                        .Elements("lang").Where(delegate(XElement item) { return item.Attribute("id").Value == lang; })
                        .Elements("item").Where(delegate(XElement item) { return item.Attribute("type") != null; })
                    select a;

            if (q.Count() > 0) {
                selects = new Dictionary<string, List<string>>();
                foreach (var item in q) {
                    string key = item.Attribute("id").Value;
                    List<string> options = new List<string>();
                    foreach (var option in item.Elements()) {
                        string value = option.Attribute("option").Value;
                        options.Add(value);
                    }

                    selects.Add(key, options);
                }
            }
            return selects;
        }
        internal static Dictionary<string, List<string>> GetSelectValues(string path, string lang) {
            Dictionary<string, List<string>> selects = null;
            XDocument doc = XDocument.Load(path);
            var q = from a in doc
                        .Element("text")
                        .Elements("lang").Where(delegate(XElement item) { return item.Attribute("id").Value == lang; })
                        .Elements("item").Where(delegate(XElement item) { return item.Attribute("type") != null; })
                    select a;

            if (q.Count() > 0) {
                selects = new Dictionary<string, List<string>>();
                foreach (var item in q) {
                    string key = item.Attribute("id").Value;
                    List<string> values = new List<string>();
                    foreach (var option in item.Elements()) {
                        string value = option.Attribute("value").Value;
                        values.Add(value);
                    }

                    selects.Add(key, values);
                }
            }
            return selects;
        }
    }
}
